package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"usershop/user_srv/internal/svc"
	"usershop/user_srv/model/mysql"
	"usershop/user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserInfoByMobileLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserInfoByMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoByMobileLogic {
	return &GetUserInfoByMobileLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserInfoByMobileLogic) GetUserInfoByMobile(in *user.GetUserInfoByMobileRequest) (*user.GetMobileInfoByResponse, error) {
	// todo: 获取获取用户信息by手机号

	u := mysql.User{}
	err := u.GetUserInfo(in.Mobile)
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		logx.Error("获取用户信息失败：", err)
		return nil, nil
	}

	return &user.GetMobileInfoByResponse{
		UserID:   int64(u.ID),
		Username: u.Username,
		Mobile:   u.Mobile,
		Password: u.Password,
		Sale:     u.Sale,
	}, nil
}
